<?php

namespace app\common\model;

use think\Cache;
use think\Model;
use traits\model\SoftDelete;
use app\common\services\UrlService;

/**
 * 课程关联题库
 */
class CourseQuestion extends Model
{
    protected $table = 'fa_yalimedia_course_question';
    
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updateTime';

    use SoftDelete;
    protected $deleteTime = 'deletetime';




    public function courseContent()
    {
        return $this->belongsTo('CourseContent','cate_id');
    }


    public function question()
    {
        return $this->belongsTo('Question','question_id');
    }
    

    public function getCreatetimeAttr($value)
    {   
        if(!empty($value)){
            return date('Y-m-d H:i:s',$value);
        }
        return '';
    }

    public function getAnswerAttr($value, $data)
    {
        if (is_array($value)) {
            return $value;
        }
        if (in_array($data['kind'], ['FILL'])) {
            return json_decode($value, true);
        }
        return $value;
    }

    public function getTitleVideoAttr($value, $data)
    {   
        if(empty($value)){
            return '';
        }
        $value = $value ?? '';
        return cdnurl($value, true);
    }

    public function getTitleVideoUrlAttr($value, $data)
    {   
        if(empty($value)){
            return '';
        }
        $value = $value ?? '';
        return cdnurl($value, true);
    }
 

    /***
     * 题目音频
     */
    public function getTitleImgAttr($value)
    {
        if(empty($value)){
            return '';
        }
        $value = $value ?? '';
        return cdnurl($value, true);
    }


    /***
     * 题目音频
     */
    public function getTitleAudioAttr($value)
    {
        if(empty($value)){
            return '';
        }
        $value = $value ?? '';
        return cdnurl($value, true);
    }


    /**
     * 例句音频
     *
     * @param [type] $value
     * @return void
     */
    public function getExampleAudioAttr($value)
    {
        if(empty($value)){
            return '';
        }
        $value = $value ?? '';
        return cdnurl($value, true);
    }


    /**
     * 解析音频
     *
     * @param [type] $value
     * @return void
     */
    public function getExplainAudioAttr($value)
    {
        if(empty($value)){
            return '';
        }
        $value = $value ?? '';
        return cdnurl($value, true);
    }
}
